package bb.lanxing.activity.login

import android.content.Intent
import android.view.View
import bb.lanxing.App
import bb.lanxing.MainActivity
import bb.lanxing.R
import bb.lanxing.manager.RemoteServiceManager
import bb.lanxing.databinding.ActivityLogoutConfirmBinding
import bb.lanxing.manager.RemoteSharedPreference
import bb.lanxing.manager.SPConstant
import bb.lanxing.manager.SharedManager
import bb.lanxing.mvp.presetner.ClubPresenter
import bb.lanxing.mvvm.base.BaseDBActivity
import bb.lanxing.network.MyHttpClient
import bb.lanxing.util.WebkitUtil

/**
 * 注销
 */
class LogoutConfirmActivity : BaseDBActivity<ActivityLogoutConfirmBinding>() {
    private lateinit var phone: String
    override fun getLayoutId(): Int {
        return R.layout.activity_logout_confirm
    }

    override fun initView(binding: ActivityLogoutConfirmBinding) {
        setupActionBar(true)
        setTitle(R.string.st_logout_account)
        phone = App.getContext().getUserPhone()
        binding.txtLogoutTitle.text =
            "请您再次确认是否注销账号 +86 " + changPhoneNumber(phone) + " 我们将在15个工作日内处理您的请求。"
        binding.btnNext.setOnClickListener { view: View? -> requestLogout() }
    }

    private fun requestLogout() {
//        Observable.create(new NetSubscribe(BiciHttpClient.requestLogout())).subscribeOn(Schedulers.io()).observeOn(Schedulers.io()).subscribe(new Observer<String>() {
//            @Override
//            public void onCompleted() {
//            }
//
//            @Override
//            public void onError(Throwable e) {
//                Intrinsics.checkNotNullParameter(e, "e");
//                App.getContext().showMessage(e.getMessage());
//            }
//
//            @Override
//            public void onNext(String str) {
//                signout();
//                startActivity(new Intent(LogoutConfirmActivity.this, MainActivity.class));
//                finish();
//            }
//        });
        // TODO: 12/28/2023 暂时不用网络请求 
        quit()
    }

    private fun quit() {
        signout()
        startActivity(Intent(this@LogoutConfirmActivity, MainActivity::class.java))
        finish()
    }

    private fun signout() {
        SharedManager.getInstance().setUserAgreement(App.getContext().signinUser.uid, false)
        SharedManager.getInstance().setUserAgreement(1, false)
        SharedManager.getInstance().isSignoutFlag = false
        SharedManager.getInstance().eventId = 0
        RemoteSharedPreference.getInstance().setValue(SPConstant.KEY_UPLOAD_LOCATION, false)
        SharedManager.getInstance().isSyncQQHealth = false
        SharedManager.getInstance().setValue(SharedManager.KEY_MAIN_CLUB_AUTH, 0)
        App.getContext().setSigninUser(null)
        //        App.getContext().stopRemoteService(WorkoutRemoteService.class);
        RemoteServiceManager.getInstance().closeBLE()
        MyHttpClient.clearCookie()
        App.getContext().showMessage(R.string.mine_profile_toast_logout_successful)
        sendMessage(R.id.msg_user_logout)
        WebkitUtil.clearCookies(this)
        ClubPresenter.postClubEvent(13, 0L, null)
    }

    private fun changPhoneNumber(str: String?): String? {
//        StringBuffer stringBuffer = new StringBuffer();
//        if (str.length() > 10) {
//            String substring = str.substring(0, 3);
//            Intrinsics.checkNotNullExpressionValue(substring, "this as java.lang.String…ing(startIndex, endIndex)");
//            stringBuffer.append(substring);
//            String substring2 = str.substring(3, 7);
//            Intrinsics.checkNotNullExpressionValue(substring2, "this as java.lang.String…ing(startIndex, endIndex)");
//            stringBuffer.append(StringsKt.replace$default(substring2, substring2, "****", false, 4, (Object) null));
//            String substring3 = str.substring(7, 11);
//            Intrinsics.checkNotNullExpressionValue(substring3, "this as java.lang.String…ing(startIndex, endIndex)");
//            stringBuffer.append(substring3);
//            String stringBuffer2 = stringBuffer.toString();
//            Intrinsics.checkNotNullExpressionValue(stringBuffer2, "{\n            val frontT…  sb.toString()\n        }");
//            return stringBuffer2;
//        }
        return str
    }
}
